package service

import (
	"github.com/pkg/errors"
	"nft-server/dao"
	"nft-server/model"
)

const (
	AdminRoleName = "admin"
	UserRoleName  = "user"
	AdminRoleId   = 1
	UserRoleId    = 0
)

var (
	AdminRole = &model.Role{
		Id:   AdminRoleId,
		Name: AdminRoleName,
	}
	UserRole = &model.Role{
		Id:   UserRoleId,
		Name: UserRoleName,
	}
)

// InitRoleTable 初始化角色表
func InitRoleTable() error {
	// 如果存在角色就不插入
	role, err := dao.GetRoleDao().GetRoleByName(AdminRoleName)
	if err != nil {
		return errors.Errorf("InitRoleTable error: %v", err)
	}
	if role == nil {
		if err := dao.GetRoleDao().InsertRole(AdminRole); err != nil {
			return errors.Errorf("InitRoleTable error: %v", err)
		}
	}
	role, err = dao.GetRoleDao().GetRoleByName(UserRoleName)
	if err != nil {
		return errors.Errorf("InitRoleTable error: %v", err)
	}
	if role == nil {
		if err := dao.GetRoleDao().InsertRole(UserRole); err != nil {
			return errors.Errorf("InitRoleTable error: %v", err)
		}
	}
	return nil
}
